package com.bytecollege.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytecollege.model.*;
import com.bytecollege.service.Impl.AdminServiceImpl;
import com.bytecollege.service.Impl.ArticleServiceImpl;
import com.bytecollege.service.Impl.UserServiceImpl;
import com.bytecollege.vo.*;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author xigua
 * @date 2022/10/19  19:01
 */
@Controller
@RequestMapping("/admin")
@Log
public class AdminController {

    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private UserServiceImpl userService;
    private int maxPage;
    @Autowired
    private ArticleServiceImpl articleService;

    /**
     * @Description: 输入/admin/跳转到管理员登录页面
     * @Param: []
     * @return: java.lang.String
     * @Author: xigua
     * @Date: 2022/10/19
     */
    @GetMapping("/")
    public String loginPage(){
        return "admin/adminLogin";
    }

    /***
     * @Description: 跳转后台首页
     * @Param: []
     * @return: java.lang.String
     * @Author: xigua
     * @Date: 2022/10/19
     */
    @GetMapping("/adminIndex")
    public String adminIndex(){
        return "admin/html/home";
    }

    /***
     * @Description: 管理员登录
     * @Param: [name, password, session, model]
     * @return: java.lang.String
     * @Author: xigua
     * @Date: 2022/10/19
     */
    @PostMapping("/login")
    public String login(@RequestParam("adminname") String name,
                        @RequestParam("adminpassword") String password,
                        HttpSession session,
                        Model model){
        //通过输入的账号查找管理员
        QueryWrapper<Admin> wrapper = new QueryWrapper<Admin>().eq("Admin_Name", name);
        Admin admin = adminService.getOne(wrapper);

        if (admin==null){
            //如果查询为空，则不存在该账户,返回登录页面重新登录
            model.addAttribute("msg","账户不存在！");
            return "admin/adminLogin";
        }else {
            //账户存在，然后比较密码是否正确
            if (admin.getAdminPwd().equals(password)){
                //相等,跳转到后台首页
                session.setAttribute("admin",admin);
                return "admin/adminIndex";
            }else {
                //密码错误，重新登录
                model.addAttribute("msg","密码错误！");
                return "admin/adminLogin";
            }
        }
    }

    /***
     * @Description: 管理员退出登录
     * @Param: [session, request, model]
     * @return: java.lang.String
     * @Author: xigua
     * @Date: 2022/10/19
     */
    @GetMapping("/logout")
    public String logout(HttpSession session, HttpServletRequest request, Model model){
        //获取当前请求的session
        session=request.getSession();
        //将原登录用户的session删除
        session.invalidate();
        model.addAttribute("msg","谢谢使用！");
        return "admin/adminLogin";
    }

    /***
    * @Description: 用户管理，显示用户并分页
    * @Param: [current, model]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/20
    */
    @GetMapping("/userList")
    public String userList(@RequestParam(value = "currentIndex",defaultValue = "1") Integer current,
                           Model model){

        //current当前页面，pageSize每页记录条数
        int pageSize=4;
        Page<User> page1=new LambdaQueryChainWrapper<>(userService.getBaseMapper()).page(new Page<>(1,pageSize));
        if (current<1){
            current=1;
        }else if (current>page1.getPages()){
            current= Math.toIntExact(page1.getPages());
        }
        Page<User> userPage=new Page<>(current,pageSize);
        Page<User> page=new LambdaQueryChainWrapper<>(userService.getBaseMapper()).page(userPage);
        //获取查询到的记录
        List<User> userList = page.getRecords();
        model.addAttribute("userList",userList);
        model.addAttribute("currentIndex",current);
        //总页数
        model.addAttribute("page",page);
        return "admin/html/userList";
    }

    /***
    * @Description: 通过关键字key查找用户
    * @Param: [key, model]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/20
    */
    @GetMapping("/search")
    public String search(@RequestParam("key") String key,Model model){
        if (key==null || "".equals(key)){
            model.addAttribute("msg","输入内容不能为空！");
            return "redirect:/admin/userList?currentIndex=1";
        }else {
            System.out.println("key------------"+key);
            List<User> userBySearch = adminService.getUserBySearch(key);
            model.addAttribute("userList",userBySearch);
            model.addAttribute("key",key);
            return "admin/html/device/device";
        }
    }

    /***
    * @Description: 封禁用户和取消封禁,2为device界面的
    * @Param: [id]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/20
    */
    @GetMapping("/banUser")
    public String banUser(@RequestParam("id") Integer id,
                          @RequestParam("currentIndex") Integer current,
                          RedirectAttributes attributes){
        adminService.banUser(id);
        attributes.addAttribute("currentIndex",current);
        return "redirect:/admin/userList";
    }
    @GetMapping("/cancelBanUser")
    public String cancelBanUser(@RequestParam("id") Integer id,
                                @RequestParam("currentIndex") Integer current,
                                RedirectAttributes attributes){
        adminService.cancelBanUser(id);
        attributes.addAttribute("currentIndex",current);
        return "redirect:/admin/userList";
    }
    @GetMapping("/banUser2")
    public String banUser2(@RequestParam("id") Integer id,@RequestParam("key") String key,
                           Model model){
        adminService.banUser(id);
        List<User> userBySearch = adminService.getUserBySearch(key);
        model.addAttribute("userList",userBySearch);
        model.addAttribute("key",key);
        return "admin/html/device/device";
    }
    @GetMapping("/cancelBanUser2")
    public String cancelBanUser2(@RequestParam("id") Integer id,@RequestParam("key") String key,
                                Model model){
        adminService.cancelBanUser(id);
        List<User> userBySearch = adminService.getUserBySearch(key);
        model.addAttribute("userList",userBySearch);
        model.addAttribute("key",key);
        return "admin/html/device/device";
    }
    
    /***
    * @Description: 管理员修改用户,2为查询结果页面修改用户
    * @Param: [adminUserVo]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/21
    */
    @PostMapping("/modUser")
    public String modUser(RedirectAttributes attributes, AdminUserVO adminUserVo){
        log.info(adminUserVo.toString());
        adminService.adminModUser(adminUserVo);
        attributes.addAttribute("currentIndex",adminUserVo.getCurrentindex());
        return "redirect:/admin/userList";
    }
    @PostMapping("/modUser2")
    public String modUser2(RedirectAttributes attributes,AdminUserVO adminUserVo){
        adminService.adminModUser(adminUserVo);
        attributes.addAttribute("key",adminUserVo.getKey());
        return "redirect:/admin/search";
    }



    /***
    * @Description: 文章审核
    * @Param: []
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/24
    */
    @GetMapping("/checkArticle")
    public String checkArticle(@RequestParam(value = "currentIndex",defaultValue = "1") Integer current,Model model){
        //current当前页面，pageSize每页记录条数
        int pageSize=5;
        Page<AdminArticleVO> page1 = new Page<>(1, pageSize);
        Page<AdminArticleVO> check = adminService.getAllToBeCheck(page1);
        maxPage= (int) check.getPages();
        if (current<1){
            current=1;
        }else if (current>maxPage){
            current= maxPage;
        }
        Page<AdminArticleVO> page = new Page<>(current, pageSize);
        Page<AdminArticleVO> toBeCheck = adminService.getAllToBeCheck(page);
        List<AdminArticleVO> articleVoList = toBeCheck.getRecords();
        model.addAttribute("articleList",articleVoList);
        model.addAttribute("currentIndex",current);
        model.addAttribute("page",toBeCheck);
        return "admin/html/toCheckArticle";
    }

    /***
    * @Description: 审核通过与审核不通过
    * @Param: [articleid]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/24
    */
    @GetMapping("/checkSuccess")
    public String checkSuccess(Integer articleid){
        adminService.checkSuccess(articleid);
        return "redirect:/admin/checkArticle";
    }
    @GetMapping("/checkFail")
    public String checkFail(Integer articleid){
        adminService.checkFail(articleid);
        return "redirect:/admin/checkArticle";
    }

    /***
    * @Description: 课程管理
    * @Param: [current, model]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/25
    */
    @GetMapping("/courseList")
    public String courseList(@RequestParam(value = "currentIndex",defaultValue = "1") Integer current,
                             Model model){

        //current当前页面，pageSize每页记录条数
        int pageSize=4;
        Page<AdminCourseVO> page1 = new Page<>(1, pageSize);
        Page<AdminCourseVO> courseVOPage = adminService.getAllCourse(page1);
        maxPage= (int) courseVOPage.getPages();
        if (current<1){
            current=1;
        }else if (current>maxPage){
            current= maxPage;
        }
        Page<AdminCourseVO> page = new Page<>(current, pageSize);
        Page<AdminCourseVO> allCourse = adminService.getAllCourse(page);
        List<AdminCourseVO> records = allCourse.getRecords();
        model.addAttribute("courseList",records);
        model.addAttribute("currentIndex",current);
        model.addAttribute("page",allCourse);
        List<Ctype> allCtype = adminService.getAllCtype();
        model.addAttribute("allCtype",allCtype);
        return "admin/html/courseList";
    }

    /***
    * @Description: 查看课程图片
    * @Param: [url]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/25
    */
    @GetMapping("/picture")
    public String picture(String url,
                          Integer currentIndex,
                          Integer courseid,
                          Model model){
        model.addAttribute("courseid",courseid);
        model.addAttribute("url",url);
        model.addAttribute("currentIndex",currentIndex);
        return "admin/html/device/picture";
    }

    /***
    * @Description: 修改课程图片
    * @Param: [courseid, coursepic, currentIndex, attributes]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/25
    */
    @PostMapping("/modCoursePic")
    public String modCoursePic(Integer courseid,String coursepic,Integer currentIndex,RedirectAttributes attributes){
        adminService.modCoursePic(courseid,coursepic);
        attributes.addAttribute("url",coursepic);
        attributes.addAttribute("courseid",courseid);
        attributes.addAttribute("currentIndex",currentIndex);
        return "redirect:/admin/picture";
    }

    /***
    * @Description: 删除课程
    * @Param: [courseid]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/25
    */
    @GetMapping("/deleteCourse")
    public String deleteCourse(Integer courseid,Integer currentIndex,RedirectAttributes attributes){
        adminService.deleteCourse(courseid);
        attributes.addAttribute("currentIndex",currentIndex);
        return "redirect:/admin/courseList";
    }

    /***
    * @Description: 添加课程
    * @Param: []
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/25
    */
    @PostMapping("/addCourse")
    public String addCourse(AdminCourseVO adminCourseVO){
        adminService.addCourse(adminCourseVO);
        return "redirect:/admin/courseList";
    }

    /***
    * @Description: 修改课程信息
    * @Param: [adminCourseVO]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/26
    */
    @PostMapping("/updateCourse")
    public String updateCourse(AdminCourseVO adminCourseVO){
        adminService.updateCourse(adminCourseVO);
        return "redirect:/admin/courseList";
    }

    /***
    * @Description: 文章管理
    * @Param: [current, model]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/26
    */
    @GetMapping("/articleList")
    public String articleList(@RequestParam(value = "currentIndex",defaultValue = "1") Integer current,
                              Model model){
        //current当前页面，pageSize每页记录条数
        int pageSize=4;
        Page<Article> page1 = new Page<>(1, pageSize);
        Page<Article> articlePage = adminService.getAllArticle(page1);
        maxPage= (int) articlePage.getPages();
        if (current<1){
            current=1;
        }else if (current>maxPage){
            current= maxPage;
        }
        Page<Article> page = new Page<>(current, pageSize);
        Page<Article> article = adminService.getAllArticle(page);
        List<Article> articleList = article.getRecords();
        model.addAttribute("articleList",articleList);
        model.addAttribute("currentIndex",current);
        model.addAttribute("page",article);
        return "admin/html/articleList";
    }

    /***
    * @Description: 管理员删除文章
    * @Param: [articleid, currentIndex, attributes]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/26
    */
    @GetMapping("/delArticle")
    public String delArticle(Integer articleid,Integer currentIndex,RedirectAttributes attributes){
        adminService.delArticle(articleid);
        attributes.addAttribute("currentIndex",currentIndex);
        return "redirect:/admin/articleList";
    }

    @GetMapping("/findArticleDetailById")
    public String findArticleDetailById(@RequestParam(value = "articleId") Integer articleId,
                                        Model model,
                                        Integer currentIndex,
                                        boolean flag){
        ArticleDetailVo article=null;
        //flag信号，为true时查找已审核的文章，为false时查找未审核的文章
        if (flag==true){
            article = articleService.getArticleById(articleId);
        }else {
            article = adminService.findArticleById(articleId);
        }
        model.addAttribute("article",article);
        model.addAttribute("currentIndex",currentIndex);
        model.addAttribute("flag",flag);
        return "admin/html/articleContent";
    }

    /***
    * @Description: 公告列表
    * @Param: [model]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/26
    */
    @GetMapping("/noticeList")
    public String noticeList(Model model){
        List<Notice> noticeList = adminService.getAllNotice();
        model.addAttribute("noticeList",noticeList);
        return "admin/html/noticeList";
    }

    /***
    * @Description: 添加公告
    * @Param: [noticeName, noticeContent]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/26
    */
    @PostMapping("/addNotice")
    public String addNotice(String noticeName,String noticeContent){
        Notice notice= Notice.builder()
                .noticeName(noticeName)
                .noticeContent(noticeContent)
                .build();
        adminService.addNotice(notice);
        return "redirect:/admin/noticeList";
    }
    /***
    * @Description: 删除公告
    * @Param: [noticeid]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/26
    */
    @GetMapping("/delNotice")
    public String delNotice(Integer noticeid){
        adminService.delNotice(noticeid);
        return "redirect:/admin/noticeList";
    }
    
    /***
    * @Description: 修改公告
    * @Param: [notice]
    * @return: java.lang.String
    * @Author: xigua
    * @Date: 2022/10/27
    */
    @PostMapping("/modNotice")
    public String modNotice(Notice notice){
        adminService.modNotice(notice);
        return "redirect:/admin/noticeList";
    }

}
